/*
 * @Author: sxb
 * @Email: sxb@mail.com
 * @Date: 2020-03-23 13:50:28
 * @Descripttion: 
 */
import { Component, OnInit } from '@angular/core';
import { DatePipe } from '@angular/common';
import { NzMessageService } from 'ng-zorro-antd/message';
import { BoxconfirmManageService } from './boxconfirm-manage.service';
import { HCommonService } from 'src/app/h-common/h-common.service';

@Component({
  selector: 'app-boxconfirm-manage',
  templateUrl: './boxconfirm-manage.component.html',
  styleUrls: ['./boxconfirm-manage.component.scss']
})
export class BoxconfirmManageComponent implements OnInit {
  boxingDetailRoles = {};

  // 装箱单列表start
  txtTaskCode: String = '';
  txtSaleCode: String = '';
  txtMaterialCode: String = '';
  txtMaterialBatch: String = '';
  txtBoxNum: String = '';
  txtBoxType: String = '';
  txtBoxPerson: String = '';
  txtBoxStartData: String = '';
  txtBoxEndData: String = '';

  checkedTaskDetailItem = null;

  pageIndex = 0;
  pageSize = 10;
  totalSize = 100;
  clickPage(index) {
    this.pageIndex = index - 1
    this.getBoxingDetailsNoDeal();
  }
  listData = [
    { id: '11', checked: false }, 
    { id: '223', checked: false }
  ];
  checkBoxDetailSingle(data){
    this.listData.forEach(item => {
      if(item.id !== data.id){ item.checked = false; }
    })
    this.checkedTaskDetailItem = data.checked ? data : null;
    this.getBoxingBucketDetail(data.id);
    // this.detailListData = [];
  }

  getBoxingDetailsNoDeal(){
    let data = {
      boxnum: this.txtBoxNum,
      boxperson: this.txtBoxPerson,
      boxtype: this.txtBoxType,
      materialCode: this.txtMaterialCode,
      productBatch: this.txtMaterialBatch,
      salecode: this.txtSaleCode,
      taskcode: this.txtTaskCode,
      startDate: this.txtBoxStartData,
      endDate: this.txtBoxEndData,
      page: this.pageIndex, 
      size: this.pageSize
    };
    this.boxconfirmService.getBoxingDetailsNoDeal(data).subscribe(res => {
      res.content.forEach(item => {
      item.checked = false;
      })
      this.totalSize = res.totalElements
      this.listData = res.content;
    })
  }
  onBoxDateChange(date) {
    this.txtBoxStartData = this.formatDate(date[0]) != null ? this.formatDate(date[0]) : '';
    this.txtBoxEndData = this.formatDate(date[1]) != null ? this.formatDate(date[1]) : '';
    this.getBoxingDetailsNoDeal();
  }
// 装箱单列表end

// 装箱单详细信息start
detailPageIndex = 0;
detailPageSize = 10;
detailTotalSize = 100;
detailListData = [];
detailClickPage(index) {
  this.detailPageIndex = index - 1
  this.getBoxingBucketDetail(this.checkedTaskDetailItem.id);
}
getBoxingBucketDetail(foid){
  let data = {
    foid:foid, 
    page: this.detailPageIndex, 
    size: this.detailPageSize,
    sort:"bucketnum,asc"};
  this.boxconfirmService.getBoxingBucketDetail(data).subscribe(res => {
    res.content.forEach(item => {
    item.checked = false;
    })
    this.detailTotalSize = res.totalElements
    this.detailListData = res.content;
  })
}
// 装箱单详细信息end

// model 3 start

isShowBox1: boolean = false;
putConfirmdetail(){
  let isExistCheckItem = false;
  this.listData.forEach(item => {
    if(item.checked){
      isExistCheckItem = true;
    }
  });
  if(!isExistCheckItem){
    this.message.warning("请选择一条装箱单信息！");
    return;
  }else{
    this.isShowBox1 = true;
  }
}
cancelBox1() {
  this.isShowBox1 = false;
}
okBox1() {
  let data = [];
  this.listData.forEach(item => {
    if(item.checked){
      data.push(item.id);
    }
  });
  this.boxconfirmService.putConfirmdetail(data).subscribe(res => {
    this.isShowBox1 = false;
    this.getBoxingDetailsNoDeal();
  })
}

// model 3 end

  // 时间格式化
  formatDate(date) {
    return this.datePipe.transform(date, 'yyyy-MM-dd HH:mm:ss');
  }

  constructor(
    private datePipe: DatePipe,
    private message: NzMessageService,
    private commonService : HCommonService,
    private boxconfirmService: BoxconfirmManageService
  ) { }

  ngOnInit() {
    /* 获取权限 */
    this.boxingDetailRoles = this.commonService.getRoles("boxingDetail");
    this.getBoxingDetailsNoDeal();
  }

}
